<?php
namespace Addons\Wechat\Controller;
class WechatController extends \Addon {
	public function _initialize() {
		parent::_initialize();
		$this->store_id = getStoreID();
		$this->storeInfo = BoxModel("addon_wx_config")->load($this->store_id);
		//dump($this->storeInfo);
		$param = array(
			'token' => $this->storeInfo['token'],
			'appID' => $this->storeInfo['appID'],
			'appsecret' => $this->storeInfo['appsecret'],
			'store_id' => $this->store_id,
		);
		$this->wechatObj = new \Common\Org\weiXin($param);
		/*****************申请成为开发者认证***************/
		if ($_GET["echostr"]) {
			if ($this->wechatObj->checkSignature()) {
				$echoStr = $_GET["echostr"];
				exit($echoStr);
			}
		}
		/******************接受信息操作******************************/
		$this->wechatObj->getMsg();
		$Content = trim($this->wechatObj->postObj->Content);
		$Event = trim($this->wechatObj->postObj->Event);
		$openid = trim($this->wechatObj->postObj->FromUserName);
		$keyName = trim($this->wechatObj->postObj->EventKey);
		$MsgType = trim($this->wechatObj->postObj->MsgType);
		$MediaId = trim($this->wechatObj->postObj->MediaId);
		$postObj = $this->wechatObj->postObj;
		/***用户消息信息***/
		/****扫描二维码*****/
		if ($Event == 'SCAN') {
			$act = $this->scan($keyName);
			exit();
		}

		/***********第一次关注************/
		if ($Event == 'subscribe') {
			$this->firstConcern($openid);
		}

		/***取消关注*****/
		if ($Event == 'unsubscribe') {
			$this->unsubscribe($openid);
		}

		/**保存地理位置*****/
		if ($Event == "LOCATION") {
			$Latitude = "" . $this->wechatObj->postObj->Latitude;
			$Longitude = "" . $this->wechatObj->postObj->Longitude;
			exit();
		}
		/**************事件信息推送*************/
		if ($keyName) {
			$this->makeNews($keyName);
			exit();
		}

	}

	/*****扫二维码***/
	function scan($keyName) {
		$openid = trim($this->wechatObj->postObj->FromUserName);
		$param = array(
			'keyName' => $keyName,
			'openid' => $openid,
		);
		$this->wx_scan_images($param);
		//	$this->wechatObj->sendText($openid, "wxScan_" . $this->storeInfo['token']);
		hook("wxScan_" . $this->storeInfo['token'], $param);
		//扫二维登录
		$loginData = S("wxLogin_" . $keyName);
		if ($loginData) {
			$user_info = BoxModel("system_member")->where(array('openid' => $openid))->find();
			$loginData['user_id'] = $user_info['user_id'];
			S("wxLogin_" . $keyName, null);
			S("wxLogin_" . $keyName, $loginData);
			$this->wechatObj->sendText($openid, "您使用微信扫一扫登录了【" . $this->storeInfo['app_name'] . "】平台");
		}
		//$this->wechatObj->sendText($openid, $keyName);

	}

	/***推送扫二维码信息**/
	function wx_scan_images($param) {
		$id = $param['keyName'];
		$openid = $param['openid'];
		$data = BoxModel("addon_wx_scan")->where(array('id' => $id))->find();
		if (!$data) {
			return;
		}
		$def_text = $data['info']; //默认回复文本
		$name = "wx_scan_" . $data['id']; //
		$data = $this->wechatObj->getNews($name);
		if (!empty($data)) {
			$this->wechatObj->sendNews($openid, $data);
		} else if ($def_text) {
			$this->wechatObj->sendText($openid, $def_text);
		}

	}

	/***************方法不存在*****/
	function __call($firstvar, $secondvar) {
		header("Content-type:text/html;charset=utf-8");
		echo "你调用的方法{$firstvar}不存在,参数";
		exit();
	}

	/*******first concern首次关注触动方法*******************/
	public function firstConcern($openid) {
		/************推送信息******/
		$this->makeNews("firstConcern");

		/****保存用户信息*****/
		$data = $this->wechatObj->UserInfo($openid);
		$user_id = Logic("BoxSns", "Member")->WxCreateMember($data);
		/*****通过渠道关注进来的*****/
		$postObj = $this->wechatObj->postObj;
		$keyName = trim($this->wechatObj->postObj->EventKey, "qrscene_");
		//$this->wechatObj->sendText($openid, "开始");
		if ($keyName) {
			//$this->wechatObj->sendText($openid, "进行中");
			$this->scan($keyName);
		}
		exit();
	}

	/*******推送图文********/
	public function makeNews($keyName = "") {
		if (!$keyName) {
			return;
		}
		$openid = trim($this->wechatObj->postObj->FromUserName);
		$this->wechatObj->sendMaterial($openid, $keyName);
	}

	/********取消订阅***********/
	private function unsubscribe($openid) {
		$r = BoxModel("system_member_store")->where("openid='$openid'")->save(array('subscribe' => 2));
	}

}
